import axios from '@/libs/axios1'
let jsonHeaders = { 'content-type': 'application/json;charset=UTF-8' }
let base = '/testplatform/productPart'

// 查询部件
export const queryProductPartList = params => {
  return axios({
    url: `${base}/page`,
    method: 'GET',
    params,
    headers: jsonHeaders,
  })
}

// 通过关联的方式升级部件
export const upgradePartByRelevance = (oldProductPartRelationId, partId) => {
  return axios({
    url: `${base}/updUpgradeAssociationV2/${oldProductPartRelationId}/${partId}`,
    method: 'PUT',
    headers: jsonHeaders,
  })
}

// 创建部件
export const addProductPart = data => {
  return axios({
    url: `${base}/add`,
    method: 'POST',
    data,
    headers: jsonHeaders,
  })
}

// 修改部件
export const updateProductPartNew = data => {
  return axios({
    url: `${base}/updPartSoftware`,
    method: 'PUT',
    data,
    headers: jsonHeaders,
  })
}

// 创建部件
export const addProductPartNew = data => {
  return axios({
    url: `${base}/savePartSoftware`,
    method: 'POST',
    data,
    headers: jsonHeaders,
  })
}

// 修改部件
export const updateProductPart = data => {
  return axios({
    url: `${base}/update`,
    method: 'PUT',
    data,
    headers: jsonHeaders,
  })
}

// 删除部件
export const deleteProductPart = data => {
  return axios({
    url: `${base}/delete`,
    method: 'DELETE',
    data,
    headers: jsonHeaders,
  })
}

// 删除产品与部件、组件的关联关系
export const deleteProductRelation = (productId, type, id) => {
  return axios({
    url: `${base}/deleteProductRelation/${productId}/${type}/${id}`,
    method: 'DELETE',
    headers: jsonHeaders,
  })
}

// 获取部件详情
export const getProductPartDetail = id => {
  return axios({
    url: `${base}/${id}/details`,
    method: 'GET',
    headers: jsonHeaders,
  })
}


// 为部件创建依赖
export const addRelationItem = (id, productId, data) => {
  return axios({
    url: `${base}/${id}/saveRelationBatch`,
    method: 'POST',
    params: {
      productId
    },
    data,
    headers: jsonHeaders,
  })
}

// 为部件删除依赖
export const deleteRelationItem = (id, productId, data) => {
  return axios({
    url: `${base}/${id}/deleteRelationBatch`,
    method: 'DELETE',
    data,
    params: {
      productId
    },
    headers: jsonHeaders,
  })
}

// 查询部件的依赖
export const getRelationItem = id => {
  return axios({
    url: `${base}/getPartDependenceRelationsById/${id}`,
    method: 'POST',
    headers: jsonHeaders,
  })
}

// 查询部件的依赖
export const addPartToProductBatch = (id, data, type) => {
  return axios({
    url: `${base}/addPartToProductBatch/${id}/${type}`,
    method: 'POST',
    data,
    headers: jsonHeaders,
  })
}

// 获取产品下依赖关系的树形菜单
export const getAllRelationTree = id => {
  return axios({
    url: `${base}/getProductDependenceTree/${id}`,
    method: 'GET',
    headers: jsonHeaders,
  })
}

// 通过组件ID查询关联的部件列表
export const getRelevancePartList = (id, params) => {
  return axios({
    url: `${base}/getPartPageByComponentId/${id}`,
    method: 'GET',
    params,
    headers: jsonHeaders,
  })
}

// 上传xml文件，解析组件
export const importBom = (partId, file) => {
  let formDatas = new FormData();
  if (file) {
    formDatas.append("file", file)
  }
  return axios({
    url: `${base}/${partId}/importBom`,
    method: 'POST',
    headers: {
      'Content-Type': "multipart/form-data",
    },
    transformRequest: [function () {
      return formDatas;
    }],
    data: formDatas
  })
}

// 获取部件漏洞统计数据
export const productPartMetrics = id => {
  return axios({
    url: `${base}/metrics/${id}`,
    method: 'GET',
    headers: jsonHeaders,
  })
}
export const queryRelativeProduct = id=>{
  return axios({
    url:`${base}/queryProductPartRelation/${id}`,
    method:'POST',
    headers:jsonHeaders
  })
}
export const queryInstallPosition = id=>{
  return axios({
    url:`${base}/queryInstallPosition/${id}`,
    method:'POST',
    headers:jsonHeaders
  })
}
